"""
数据模型定义
"""

from pydantic import BaseModel, Field
from typing import Optional, List, Dict, Any
from datetime import datetime
from enum import Enum

class TaskStatus(str, Enum):
    """任务状态枚举"""
    PENDING = "pending"      # 待处理
    PROCESSING = "processing"  # 处理中
    COMPLETED = "completed"    # 已完成
    FAILED = "failed"         # 失败

class ProcessingMode(str, Enum):
    """处理模式枚举"""
    AUTO = "auto"             # 自动模式
    INTERACTIVE = "interactive"  # 交互模式
    RULES = "rules"           # 规则引擎模式

class Priority(str, Enum):
    """优先级枚举"""
    P0 = "P0"  # 最高优先级
    P1 = "P1"  # 高优先级
    P2 = "P2"  # 中优先级
    P3 = "P3"  # 低优先级

# 请求模型
class AudioUploadRequest(BaseModel):
    """音频上传请求"""
    processing_mode: ProcessingMode = ProcessingMode.AUTO
    chunk_size: Optional[int] = Field(default=900, description="大文件分割大小（秒）")
    timeout: Optional[int] = Field(default=3600, description="处理超时时间（秒）")

class TaskQueryRequest(BaseModel):
    """任务查询请求"""
    task_id: str
    include_details: bool = Field(default=False, description="是否包含详细信息")

# 响应模型
class TaskResponse(BaseModel):
    """任务响应"""
    task_id: str
    status: TaskStatus
    message: str
    created_at: datetime
    updated_at: Optional[datetime] = None
    progress: Optional[float] = Field(default=0.0, description="处理进度 0-100")

class FileInfo(BaseModel):
    """文件信息"""
    filename: str
    size: int
    duration: Optional[float] = None
    format: str

class Speaker(BaseModel):
    """说话人信息"""
    speaker_id: str
    role: Optional[str] = None
    segments_count: int

class Requirement(BaseModel):
    """需求信息"""
    id: str
    requirement_text: str
    requirement_type: str
    priority: Priority
    speaker_role: str
    confidence: float
    timestamp: str

class Conflict(BaseModel):
    """需求冲突"""
    requirement1_id: str
    requirement2_id: str
    conflict_type: str
    description: str

class ProcessingResult(BaseModel):
    """处理结果"""
    task_id: str
    status: TaskStatus
    file_info: FileInfo
    processing_time: float
    stages: Dict[str, Any]
    
    # Stage1 结果
    transcript_file: Optional[str] = None
    total_segments: Optional[int] = None
    speaker_count: Optional[int] = None
    
    # Stage2 结果
    analysis_file: Optional[str] = None
    prd_file: Optional[str] = None
    meeting_notes_file: Optional[str] = None
    total_requirements: Optional[int] = None
    speakers: Optional[List[Speaker]] = None
    requirements: Optional[List[Requirement]] = None
    conflicts: Optional[List[Conflict]] = None
    
    # Stage3 结果
    report_files: Optional[Dict[str, str]] = None
    final_summary: Optional[Dict[str, Any]] = None
    
    error_message: Optional[str] = None
    created_at: datetime
    completed_at: Optional[datetime] = None

class ErrorResponse(BaseModel):
    """错误响应"""
    error: str
    message: str
    details: Optional[Dict[str, Any]] = None
    timestamp: datetime = Field(default_factory=datetime.now)

class HealthResponse(BaseModel):
    """健康检查响应"""
    status: str
    service: str
    version: str = "1.0.0"
    timestamp: Optional[float] = None
    uptime: Optional[float] = None

class DetailedHealthResponse(BaseModel):
    """详细健康检查响应"""
    status: str
    service: str
    version: str = "1.0.0"
    timestamp: float
    uptime: float
    overall_status: str
    system: Optional[Dict[str, Any]] = None
    models: Optional[Dict[str, Any]] = None
    storage: Optional[Dict[str, Any]] = None
    service_stats: Optional[Dict[str, Any]] = None
    directories: Optional[Dict[str, Any]] = None

class StatsResponse(BaseModel):
    """统计信息响应"""
    total_tasks: int
    completed_tasks: int
    failed_tasks: int
    active_tasks: int
    average_processing_time: Optional[float] = None
    total_audio_duration: Optional[float] = None